app.controller("z-lactation-c", [ '$scope','$rootScope','$z-http','$sce','z-lactation-s','z-dictionary-s','z-corporation-s','z-department-s','z-punchTime-s','z-attendance-s','z-edit-menu-s','z-employee-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,DictionaryService,CorporationService,DepartmentService,PunchTimeService,AttendanceService,MenuService,EmployeeService,$filter) {
		$scope.viewModel = {};
		$scope.editModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";	
		//编辑使用
		$scope.editModel = {};
		$scope.editModel.maternityLeave={};
		$scope.editModel.title="";
		//日期控件
		$scope.editModel.dateOptions = {
				formatYear: 'yy',
			    startingDay: 1,
			    showWeeks:false
		};
		//日期控件
		$scope.editModel.format = "yyyy/MM/dd";
		//日期启动开关
		$scope.editModel.editHbornStartDateOpnened= false;
		//起止日期选择
		$scope.editModel.editHbornStartDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			$scope.editModel.editHbornStartDateOpnened = !$scope.editModel.editHbornStartDateOpnened;
		};
		$scope.viewModel.effectiveDate = new Date();
		//查询页面名称
		MenuService.getMenuByUrl({url:'lactation'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		//加载地区数据
		$scope.viewModel.bindSelectPlace = function(){
			AttendanceService.getDictionaryListBydicCls({
				dicCls:"7"
			},
			function(data){
				$scope.viewModel.PlaceData = data;
				$scope.editModel.PlaceData = data;
			},function(error) {
						
			});
		};
		$scope.viewModel.bindSelectPlace();
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.employeeList=[];
			if($scope.viewModel.employeeNo!=null){
				var employeeNoData = $scope.viewModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewModel.employeeList.push(employeeNoData[i]);
				}
			}
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getLactationList({
				pageCount : $scope.viewModel.perPage,
				pageNo : $scope.viewModel.rowCount,
				employeeList:$scope.viewModel.employeeList,
			},
			function(data){
				$scope.viewModel.dataList = data.lactationBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		$scope.viewModel.bindData();
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
		};
		
		//新增
		$scope.viewModel.add = function(){
			$scope.viewModel.load();
			$scope.editModel.title="新增哺乳假额度";
			$scope.editModel.type = true;
			$scope.editModel.lactation={};
			$scope.editModel.lactation.hours = "1";
			$scope.editModel.lactation.requestType=10;
			//验证不能为空
			$scope.editForm.employeeName.$dirty=false;
			$scope.editForm.hbornStartDate.$dirty=false;
			$scope.editForm.hours.$dirty=false;
		};
		//修正
		$scope.viewModel.update=function(lactation){
			$scope.viewModel.load();
			$scope.editModel.title="修改哺乳假额度";
			$scope.editModel.type = false;
			$scope.editModel.lactation=angular.copy(lactation);
			if(lactation.hbornStartDate!=null){
				$scope.editModel.lactation.hbornStartDate=new Date(lactation.hbornStartDate);
			}
			if(lactation.endDate!=null){
				$scope.editModel.lactation.endDate=new Date(lactation.endDate);
			}
		};
		//验证
		$scope.viewModel.requiredForm= function() {
            if (!$scope.editForm.$valid) {
            	if(!$scope.editForm.employeeName.$dirty){
            		$scope.editForm.employeeName.$dirty=true;
            	}
            	if(!$scope.editForm.hbornStartDate.$dirty){
            		$scope.editForm.hbornStartDate.$dirty=true;
            	}
            	if(!$scope.editForm.hours.$dirty && $scope.editModel.lactation.hours == ""){
            		$scope.editForm.hours.$dirty=true;
            	}
                return true;
            }else{
            	if($scope.editModel.lactation.employeeId==null){
            		$rootScope.alertError("员工信息未选择，请选择。");
            		return true;
            	}else{
            		return false;
            	}
            }
        };
        // 日期选择,自动生成假期结束日期方法
        $scope.editModel.dateChange = function(){
        		$scope.editModel.lactation.endDate = new Date(
        				($scope.editModel.lactation.hbornStartDate.getFullYear()+1)
        				+ "/" + ($scope.editModel.lactation.hbornStartDate.getMonth()+1) +
        				"/" + $scope.editModel.lactation.hbornStartDate.getDate());
        }
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.requiredForm()){
				return;
			}
			$scope.viewModel.isLoading = true;
			if($scope.editModel.type){
				Service.saveLactation($scope.editModel.lactation,
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("新增成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
			}else{
				Service.updateLactation($scope.editModel.lactation,
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("修改成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
			}
		};
		// 删除
		$scope.viewModel.del = function(lactation){
			$rootScope.alertConfirm("确定删除吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deleteLactation({
					lactationId:lactation.lactationId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		}
		/*   人员选择   */
		$scope.viewEmployeeModel={};
		$scope.viewEmployeeModel.perPage = "10";
		$scope.viewEmployeeModel.rowCount = "1";
		// 人员选择按钮
		$scope.editModel.chooseUpde = function(){
			$scope.viewEmployeeModel.show=true;
			$scope.viewEmployeeModel.title="员工选择";
			$scope.viewEmployeeModel.employeeIsSeparated=null;
			$scope.viewEmployeeModel.regionType=null;
			$scope.viewEmployeeModel.employeeNo=null;
			$scope.viewEmployeeModel.roleName=null;
			$scope.viewEmployeeModel.departmentList=[];
			$scope.viewEmployeeModel.corporationList=[];
			$scope.viewEmployeeModel.employeeList=[];
			$scope.viewEmployeeModel.level=null;
			$(".multiselect-selected-text").text("无");
			//下拉多选框长度
			$scope.multiSelectMaxHeight=300;
			//加载信息
			$scope.viewEmployeeModel.bindSelectRegionTypeData = function(){
				//查询公司
				CorporationService.getCorporationList({
					fromDate:$scope.viewModel.effectiveDate,
				},
				function(data){
					var site = [];  
			        $.each(data, function(index, values) {  
			            site.push({  
			                label : "("+values.corporationCode+")"+values.corporationName,  
			                value : values.corporationCode  
			            });  
			        });  
			        $('#lasCorporation').multiselect('dataprovider',site);
				},function(error) {
					
				});
			};
			$scope.viewEmployeeModel.bindSelectRegionTypeData();
			
		
			/*******使用为下拉带勾选控件*********/
			//员工级别
			$('#laslevel').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			//公司
			$('#lasCorporation').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			//公司选中事件
			$("#lasCorporation").change(function(){  
				var value=$(this).val();
			    var data={};
			    data.corporationCodeList=value;
			    data.fromDate = $scope.viewModel.effectiveDate;
			    if(value==null){
			    	var site = [];  
			    	$('#lasDepartmentName').multiselect('dataprovider',site);
			    	$scope.viewEmployeeModel.departmentList=[];
			    }else{
			    	//查询部门
				    DepartmentService.getDepartmentListByCorporationCodeList(data,
					function(data){
				    	var site = [];  
				        $.each(data, function(index, values) {  
				            site.push({  
				                label : "("+values.departmentCode+")"+values.departmentName,  
				                value : values.departmentCode  
				            });  
				        });  
				        $('#lasDepartmentName').multiselect('dataprovider',site);
					},function(error) {
						
					});
			    }
	        });  
			
			//部门
			$('#lasDepartmentName').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			EmployeeService.getEmployeeList({
				rowCount : $scope.viewEmployeeModel.perPage,
				perPage : $scope.viewEmployeeModel.rowCount,
				sex:"2",
				fromDate:$scope.viewModel.effectiveDate,
			},
			function(data){
				$scope.viewEmployeeModel.dataEmployeeList = data.employeeBeanList;
				$scope.viewEmployeeModel.totalData = data.pageTotal;
				$scope.viewEmployeeModel.isLoading=false;
			},function(error) {
				$scope.viewEmployeeModel.isLoading=false;
			});
			
		}
		//关闭加载
		$scope.viewEmployeeModel.unLoad=function(){
			$scope.viewEmployeeModel.show=false;
			$scope.editModel.title="新增哺乳假额度";
		};
		//是否选中
		$scope.viewEmployeeModel.isInAssignmentArr=function(employee){
			if($scope.editModel.lactation.employeeId == employee.employeeId){
				return true;
			}
			return false;
		};
		//加载数据
		$scope.viewEmployeeModel.bindData = function(){
			$scope.viewEmployeeModel.employeeList=[];
			if($scope.viewEmployeeModel.employeeNo!=null){
				var employeeNoData = $scope.viewEmployeeModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewEmployeeModel.employeeList.push(employeeNoData[i]);
				}
			}
			$scope.viewModel.isLoading=true;
			//获取基本信息
			EmployeeService.getEmployeeList({
				employeeList:$scope.viewEmployeeModel.employeeList,
				departmentList:$scope.viewEmployeeModel.departmentList,
				corporationList:$scope.viewEmployeeModel.corporationList,
				employeeLevelList:$scope.viewEmployeeModel.employeeLevelList,
				employeeIsSeparated:"0",
				perPage:$scope.viewEmployeeModel.rowCount,
				rowCount:$scope.viewEmployeeModel.perPage,
				fromDate:$scope.viewModel.effectiveDate,
				sex:"2",
			},
			function(data){
				$scope.viewEmployeeModel.dataEmployeeList = data.employeeBeanList;
				$scope.viewEmployeeModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		// 点击选择指定人员按钮
		$scope.viewEmployeeModel.employee = function(lactation){
			$scope.viewEmployeeModel.show=false;
			$scope.viewModel.show=true;
			$scope.editModel.lactation.employeeId = lactation.employeeId;
			$scope.editModel.lactation.employeeNo = lactation.employeeNo;
			$scope.editModel.lactation.chineseName = lactation.chineseName;
		}
		// 员工清空
		$scope.editModel.chooseDelete = function(){
			$scope.editModel.lactation.employeeId = null;
		}
} ]);